package com.cc.service.imp;

import com.cc.entity.User;
import com.cc.entity.UserExample;
import com.cc.entity.UserExample.Criteria;
import com.cc.mapper.UserMapper;
import com.cc.service.IUserService;
import com.github.pagehelper.PageHelper;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Service
public class UserServiceImpl implements IUserService {
    @Resource
    private UserMapper userMapper;
    @Override
    public List<User> list(int page) {
        PageHelper.startPage(page,8);
        List<User> userList = userMapper.selectByExample(null);
        if(userList==null&& userList.size()==0){
            throw new RuntimeException("查询用户列表信息失败....");
        }
        return userList;
    }

    @Override
    public boolean save(User user) {
        int result = userMapper.insertSelective(user);
        return result==0?false:true;
    }
    @Override
    public User detail(int id) {
        return userMapper.selectByPrimaryKey(id);
    }

    @Override
    public boolean delete(int id) {
        return userMapper.deleteByPrimaryKey(id)>0?true:false;
    }
    @Override
    public boolean update(User user) {
        return userMapper.updateByPrimaryKeySelective(user)>0?true:false;
    }

    @Override
    public boolean batchDelete(int[] ids) {
        UserExample example  =new UserExample();
        Criteria criteria = example.createCriteria();
        List<Integer> idList = new ArrayList<Integer>();
        for(int id : ids) {
            idList.add(id);
        }
        criteria.andIdIn(idList);//where id in(47,30)
        int result  =userMapper.deleteByExample(example);
        return result>0?true:false;
    }
}
